package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.system.domain.IdCardInfo;
import com.ruoyi.system.service.IdCardInfoService;


/**
 * 身份证识别信息Controller
 *
 * @author ruoyi
 * @date 2025-08-26
 */
@RestController
@RequestMapping("/system/idcard")
public class IdCardInfoController extends BaseController
{
    @Autowired
    private IdCardInfoService idCardInfoService;

    /**
     * 查询身份证识别信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:idcard:list')")
    @GetMapping("/list")
    public TableDataInfo list(IdCardInfo idCardInfo)
    {
        startPage();
        List<IdCardInfo> list = idCardInfoService.selectIdCardInfoList(idCardInfo);
        return getDataTable(list);
    }

    /**
     * 导出身份证识别信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:idcard:export')")
    @Log(title = "身份证识别信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IdCardInfo idCardInfo)
    {
        List<IdCardInfo> list = idCardInfoService.selectIdCardInfoList(idCardInfo);
        ExcelUtil<IdCardInfo> util = new ExcelUtil<IdCardInfo>(IdCardInfo.class);
        util.exportExcel(response, list, "身份证识别信息数据");
    }

    /**
     * 获取身份证识别信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:idcard:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(idCardInfoService.selectIdCardInfoById(id));
    }

    /**
     * 新增身份证识别信息
     */
    @PreAuthorize("@ss.hasPermi('system:idcard:add')")
    @Log(title = "身份证识别信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IdCardInfo idCardInfo)
    {
        return toAjax(idCardInfoService.insertIdCardInfo(idCardInfo));
    }

    /**
     * 修改身份证识别信息
     */
    @PreAuthorize("@ss.hasPermi('system:idcard:edit')")
    @Log(title = "身份证识别信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IdCardInfo idCardInfo)
    {
        return toAjax(idCardInfoService.updateIdCardInfo(idCardInfo));
    }

    /**
     * 删除身份证识别信息
     */
    @PreAuthorize("@ss.hasPermi('system:idcard:remove')")
    @Log(title = "身份证识别信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(idCardInfoService.deleteIdCardInfoByIds(ids));
    }

    /**
     * 上传并识别身份证
     */
    @PreAuthorize("@ss.hasPermi('system:idcard:recognize')")
    @Log(title = "身份证识别", businessType = BusinessType.OTHER)
    @PostMapping("/recognize")
    public AjaxResult recognize(MultipartFile file)
    {
        try {
            IdCardInfo idCardInfo = idCardInfoService.uploadAndRecognizeIdCard(file);
            return success(idCardInfo);
        } catch (Exception e) {
            return error("识别失败：" + e.getMessage());
        }
    }
}